package com.example.demo.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    UserMapper userMapper;
//    @RequestMapping("/login")
//    public String loginin(User user){
//        System.out.println(userService.login(user));
//        if (userService.login(user)){
//            return "登陆成功";
//        }else {
//            return "登陆失败";
//        }
//    }


    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(@RequestBody User user){
        System.out.println("controller:"+user);
        if (userService.login(user)){
            return "登陆成功";
        }else {
            return "登陆失败";
        }
    }

    @RequestMapping(value = "/hello1",method = RequestMethod.GET)
//    等价于@PostMapping("hello1")
    public String hello1(String name){
        return "hello"+name;
    }


    //@RequestParam注解是用于参数映射的，且必须强制传递参数
    @RequestMapping(value = "/hello2",method = RequestMethod.GET)
    public String hello2(@RequestParam("name") String id){
        return "hello"+id;
    }

    //如果使用了@RequestParam注解,且不想强制传递参数，可以使用required = false
    @RequestMapping(value = "/hello3",method = RequestMethod.GET)
    public String hello3(@RequestParam(value = "name",required = false) String id){
        return "hello"+id;
    }

    @RequestMapping(value = "/hello4",method = RequestMethod.POST)
    public String hello4(){
        return "POST请求";
    }

    @RequestMapping(value = "/hello5",method = RequestMethod.POST)
    public String hello5(String id,String name){
        System.out.println(id);
        System.out.println(name);
        return "POST请求";
    }

    @RequestMapping(value = "/hello6",method = RequestMethod.POST)
    public String hello6(User user){
        System.out.println(user);
        return "POST请求";
    }

    //@RequestBody注解专门用于接受JSON对象
    @RequestMapping(value = "/hello7",method = RequestMethod.POST)
    public String hello7(@RequestBody User user){
        System.out.println(user);
        return "POST请求";
    }

    @GetMapping("/findA11")
    public IPage findA11(){
        Page<User> page = new Page<>(0,2);
        return userMapper.selectPage (page, null);
    }
}
